package vip.xiaonuo.vein.websocket.ack;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.devicehistory.entity.DeviceHistory;
import vip.xiaonuo.facedoor.modular.devicehistory.service.DeviceHistoryService;
import vip.xiaonuo.facedoor.modular.deviceset.entity.DwDeviceSet;
import vip.xiaonuo.facedoor.modular.deviceset.service.DwDeviceSetService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.memberfeatures.entity.MemberFeatures;
import vip.xiaonuo.facedoor.modular.memberfeatures.service.MemberFeaturesService;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.modular.orgdevice.entity.OrgDevice;
import vip.xiaonuo.facedoor.modular.orgdevice.service.OrgDeviceService;
import vip.xiaonuo.facedoor.modular.redis.RedisKey;
import vip.xiaonuo.vein.controller.vo.QueryRecordDto;
import vip.xiaonuo.vein.model.ResData;
import vip.xiaonuo.vein.modular.veindeviceevent.entity.VeinDeviceEvent;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventPersonTypeEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventStatusEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.service.VeinDeviceEventService;
import vip.xiaonuo.vein.modular.veinpalmdeviceuserrelate.entity.VeinPalmDeviceUserRelate;
import vip.xiaonuo.vein.modular.veinpalmdeviceuserrelate.enums.VeinPalmDeviceRelateStatusEnum;
import vip.xiaonuo.vein.modular.veinpalmdeviceuserrelate.service.VeinPalmDeviceUserRelateService;
import vip.xiaonuo.vein.service.WsReplyService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class AckService {


    public static Map<Integer, String> resultCode = new HashMap<Integer, String>() {
        {
            put(0, "成功");
            put(1, "校验失败");
            put(2, "Json解析失败");
            put(3, "包⻓度不匹配");
            put(4, "会话流⽔号重复");
            put(5, "⽤户基本信息操作失败");
            put(6, "⽣物特征信息操作失败");
            put(7, "⻔禁权限操作失败");
            put(8, "指令操作失败");
            put(9, "查询起始时间解析失败");
            put(10, "⽐对失败 ");
            put(11, "⽤户数量不⾜");
            put(99, "其它未知失败");
        }
    };

    Integer SUCCESS = 1;
    Integer FAIL = 2;

    @Autowired
    private RedissonClient redissonClient;


    @Resource
    private MemberFeaturesService featuresService;

    @Autowired
    private VeinDeviceEventService veinDeviceEventService;

    @Autowired
    private MemberDeviceService memberDeviceService;

    @Resource
    private MemberUserService memberUserService;

//    @Resource
//    private DeviceLogService deviceLogService;

    @Resource
    private DeviceBaseService deviceBaseService;

    @Resource
    private OrgDeviceService orgDeviceService;

    @Resource
    private DwDeviceSetService dwDeviceSetService;

    @Resource
    private DeviceHistoryService deviceHistoryService;

    @Resource
    private VeinPalmDeviceUserRelateService deviceUserRelateService;

    @Resource
    private WsReplyService replyService;

    @Transactional(rollbackFor = Exception.class)
    public void ack(String message) {
        String sn = message.substring(4, 8);
        QueryWrapper<VeinDeviceEvent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VeinDeviceEvent::getStatus, VeinDeviceEventStatusEnum.EXECUTE.getCode());
        queryWrapper.last("and serial_number like '____".concat(sn) + "'");
        VeinDeviceEvent event = veinDeviceEventService.getOne(queryWrapper);
        if (event == null) {
            log.error("找不到下发的序列任务【{}】", message);
            return;
        }
        try {
            event.setResponse(message);
            // 新增人员
            if (ObjectUtil.equal(event.getType(), VeinDeviceEventPersonTypeEnum.ADD_PERSON.getCode())) {
                addPerson(message, event);
            }
            // 修改人员
            else if (ObjectUtil.equal(event.getType(), VeinDeviceEventPersonTypeEnum.UPDATE_PERSON.getCode())) {
                updatePerson(message, event);
            }
            //  删除人员
            else if (ObjectUtil.equal(event.getType(), VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode())) {
                deletePerson(message, event);
            }
            //  批量删除人员
            else if (ObjectUtil.equal(event.getType(), VeinDeviceEventPersonTypeEnum.DELETE_BATCH_MEMBER.getCode())) {
                batchDeletePerson(message, event);
            }
            //  通行记录获取
            else if (ObjectUtil.equal(event.getType(), VeinDeviceEventPersonTypeEnum.REQUEST_RECORD.getCode())) {
                requestRecord(message, event);
            }
            //  特征值下发
            else if (ObjectUtil.equal(event.getType(), VeinDeviceEventPersonTypeEnum.FEATURE_ISSUED.getCode())) {
                featureIssued(message, event);
            }
            veinDeviceEventService.updateById(event);
        } catch (Exception e) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("本地错误！" + ExceptionUtil.getMessage(e));
            veinDeviceEventService.updateById(event);
            e.printStackTrace();
            log.error("设备回复本地发生错误【{}】", message);
        }

    }

    private void featureIssued(String message, VeinDeviceEvent event) {
        if (message.length() < 8) {
            log.debug("返回数据无需处理：{}", message);
            return;
        }

        MemberUser user = memberUserService.getById(event.getBody());

        if (user == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            log.error("人员不存在！ 跳过执行 人员id【{}】", event.getBody());
            event.setFailReason("人员不存在！");
            return;
        }

        DeviceBase deviceBase = deviceBaseService.getById(event.getDeviceId());

        if (deviceBase == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("设备已经不存在！");
            log.error("设备已经不存在！ 跳过执行 设备id【{}】", event.getDeviceId());
            return;
        }

        String serialNo = message.substring(4, 8);
        VeinPalmDeviceUserRelate relate = deviceUserRelateService.getOne(new LambdaQueryWrapper<VeinPalmDeviceUserRelate>()
                .eq(VeinPalmDeviceUserRelate::getSn, serialNo)
                .eq(VeinPalmDeviceUserRelate::getDeviceId, deviceBase.getId()));
        if (relate == null) {
            log.debug("请求流水号：{}对应的待处理数据不存在", serialNo);
            return;
        }

        try {
            String result = message.substring(8);
            ResData resData = JSON.parseObject(result, ResData.class);
            int resultCode = resData.getReturn_value();
            if (resultCode == 0) {// 处理完成
                relate.setResult(null);
                relate.setStatus(VeinPalmDeviceRelateStatusEnum.DONE.getCode());
                event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_SUCCESS.getCode());
            } else { // 处理失败，记录失败原因
                String reason = AckService.resultCode.containsKey(resultCode) ? AckService.resultCode.get(resultCode) : "未知错误";

                if (relate.getStatus() == VeinPalmDeviceRelateStatusEnum.ADD.getCode()) {
                    relate.setStatus(VeinPalmDeviceRelateStatusEnum.ADD_FAIL.getCode());
                } else if (relate.getStatus() == VeinPalmDeviceRelateStatusEnum.UPDATE.getCode()) {
                    relate.setStatus(VeinPalmDeviceRelateStatusEnum.UPDATE_FAIL.getCode());
                }
                relate.setResult(reason);

                event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
                event.setFailReason(reason);
            }
        } catch (Exception e) {
            relate.setResult("本地错误！" + ExceptionUtil.getMessage(e));
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("本地错误！" + ExceptionUtil.getMessage(e));
            e.printStackTrace();
            log.error("设备回复本地发生错误【{}】", message);
        }finally {
            deviceUserRelateService.updateById(relate);
            // 修改event表的数据
            veinDeviceEventService.updateById(event);
        }
    }

    private void requestRecord(String message, VeinDeviceEvent event) throws ParseException {
        QueryRecordDto queryRecordDto = JSON.parseObject(message.substring(8), QueryRecordDto.class);
        List<QueryRecordDto.AcTaRecordDTO> acTaRecord = queryRecordDto.getAcTaRecord();
        Long deviceId = Long.parseLong(event.getBody().split("_")[0]);
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        if (deviceBase == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("设备已经不存在！");
            log.error("设备已经不存在！ 跳过执行 设备id【{}】", deviceId);
            return;
        }
        if (CollectionUtil.isNotEmpty(acTaRecord)) {
            for (QueryRecordDto.AcTaRecordDTO acTaRecordDTO : acTaRecord) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date time = simpleDateFormat.parse(acTaRecordDTO.getTime());
                MemberUser memberUser = memberUserService.getById(acTaRecordDTO.getUserUuid().substring(1));
                if (memberUser == null) {
                    event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
                    log.error("人员不存在！ 跳过执行 人员id【{}】", acTaRecordDTO.getUserUuid());
                    event.setFailReason("人员不存在！");
                    continue;
                }
                long count = deviceHistoryService.count(new LambdaQueryWrapper<DeviceHistory>().eq(DeviceHistory::getDeviceBaseId, deviceId).eq(DeviceHistory::getMemberId, Long.parseLong(acTaRecordDTO.getUserUuid().substring(1))).eq(DeviceHistory::getDeviceCurrentDate, time));

                if (count != 0) {
                    log.error("【{}】收到重复【{}】记录跳过", deviceBase.getName(), message);
                    continue;
                }
                DeviceHistory deviceHistory = new DeviceHistory();
                deviceHistory.setMemberId(memberUser.getId());
                deviceHistory.setMemberName(memberUser.getName());
                deviceHistory.setDeviceCurrentDate(time);
                deviceHistory.setMemberJobNum(memberUser.getJobNum());
                deviceHistory.setDeviceName(deviceBase.getName());
                deviceHistory.setImage(memberUser.getPhoto());
                deviceHistory.setOrgId(memberUser.getOrgId());
                deviceHistory.setDeviceBaseId(deviceBase.getId());
                deviceHistory.setTemperature(acTaRecordDTO.getTemperature());
                deviceHistoryService.save(deviceHistory);
            }

        }
        event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_SUCCESS.getCode());
        RBucket<String> lastSyncTime = redissonClient.getBucket(RedisKey.VEIN_QUERY_RECARD_LAST_TIME_PRE.concat(deviceBase.getRegistrationCode()));
        lastSyncTime.set(event.getBody().split("_")[1]);
    }


    private void deletePerson(String message, VeinDeviceEvent event) {
        ResData resData = JSON.parseObject(message.substring(8), ResData.class);
        Long memberDeviceId = Long.parseLong(event.getBody());
        MemberDevice memberDevice = memberDeviceService.getById(memberDeviceId);
        if (memberDevice == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("设备关联关系已经不存在！");
            log.error("人员设备关联关系已经不存在！ 跳过执行 关联id【{}】", memberDeviceId);
            return;
        }
        MemberUser memberUser = memberUserService.getById(memberDevice.getMemberId());
        if (memberUser == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            log.error("人员不存在！ 跳过执行 人员id【{}】", memberDevice.getMemberId());
            event.setFailReason("人员不存在！");
            memberDevice.setFailReason("删除人员发生错误：人员不存在" + message);
            memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog("", "", message, FAIL, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode());
            return;
        }

        DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
        if (deviceBase == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("设备已经不存在！");
            log.error("设备已经不存在！ 跳过执行 设备id【{}】", memberDevice.getDeviceId());
            memberDevice.setFailReason("删除人员发生错误：设备已经不存在" + message);
            memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog(memberUser.getName(), "", message, FAIL, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode());
            return;
        }
        // 成功
        if (ObjectUtil.equal(resData.getReturn_value(), 0)) {
            memberDeviceService.removeById(memberDevice.getId());
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_SUCCESS.getCode());
            long count = memberDeviceService.count(new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getMemberId, memberDevice.getMemberId()));
            if (count == 0 && ObjectUtil.equal(memberUser.getDelFlag(), "1")) {
                memberUserService.removeById(memberDevice.getMemberId());
            }
//            deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message, SUCCESS, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode());
            // 失败
        } else {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("删除人员发生错误：" + message);
            memberDevice.setFailReason("删除人员发生错误：" + message);
            memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message, FAIL, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode());
        }
    }


    private void batchDeletePerson(String message, VeinDeviceEvent event) {
        ResData resData = JSON.parseObject(message.substring(8), ResData.class);
        String[] memberDeviceIds = event.getBody().split(",");
        DeviceBase deviceBase = deviceBaseService.getById(event.getDeviceId());
        if (deviceBase == null) {
            log.error("设备已经不存在！ 跳过执行 设备id【{}】", event.getDeviceId());
            memberDeviceService.removeById(event.getId());
            return;
        }
        // 成功
        if (ObjectUtil.equal(resData.getReturn_value(), 0)) {
            for (String memberId : memberDeviceIds) {
                MemberDevice memberDevice = memberDeviceService.getOne(new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getDeviceId,deviceBase.getId()).eq(MemberDevice::getMemberId,Long.parseLong(memberId.substring(1))));
                if (memberDevice == null) {
                    log.error("人员设备关联关系已经不存在！ 跳过执行 关联id【{}】", memberId);
                    continue;
                }
                MemberUser memberUser = memberUserService.getById(memberDevice.getMemberId());
                if (memberUser == null) {
                    memberDeviceService.removeById(memberDevice.getId());
                    continue;
                }
                memberDeviceService.removeById(memberDevice.getId());
                event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_SUCCESS.getCode());
//                deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message, SUCCESS, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode());
            }
            // 是否删除设备
            long count = memberDeviceService.count(new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getDeviceId, deviceBase.getId()));
            if (count == 0 && ObjectUtil.equal(deviceBase.getDelFlag(), "1")) {
                deviceBaseService.removeById(deviceBase.getId());
                orgDeviceService.remove(new LambdaQueryWrapper<OrgDevice>().eq(OrgDevice::getDeviceId, deviceBase.getId()));
                memberDeviceService.remove(new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getDeviceId, deviceBase.getId()));
                dwDeviceSetService.remove(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId, deviceBase.getId()));
            }
            // 失败
        } else {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("批量删除人员发生错误：" + message);
//            deviceLogService.addMemberLog("批量删除人员", "", message, FAIL, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode());
        }
    }


    private void updatePerson(String message, VeinDeviceEvent event) {
        ResData resData = JSON.parseObject(message.substring(8), ResData.class);
        Long memberDeviceId = Long.parseLong(event.getBody());
        MemberDevice memberDevice = memberDeviceService.getById(memberDeviceId);
        if (memberDevice == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            log.error("人员设备关联关系已经不存在！ 跳过执行 关联id【{}】", memberDeviceId);
            event.setFailReason("人员设备关联关系已经不存在！");
            return;
        }
        MemberUser memberUser = memberUserService.getById(memberDevice.getMemberId());
        if (memberUser == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("人员已经不存在！");
            log.error("人员已经不存在！ 跳过执行 人员id【{}】", memberDevice.getMemberId());
            memberDevice.setFailReason("修改人员发生错误：人员已经不存在" + message);
            memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog("", "", message, FAIL, VeinDeviceEventPersonTypeEnum.UPDATE_PERSON.getCode());
            return;
        }

        DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
        if (deviceBase == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("设备已经不存在！");
            log.error("设备已经不存在！ 跳过执行 设备id【{}】", memberDevice.getDeviceId());
            memberDevice.setFailReason("修改人员发生错误：设备已经不存在" + message);
            memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog(memberUser.getName(), "", message, FAIL, VeinDeviceEventPersonTypeEnum.UPDATE_PERSON.getCode());
            return;
        }
        // 成功
        if (ObjectUtil.equal(resData.getReturn_value(), 0)) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_SUCCESS.getCode());
            memberDevice.setFailReason(null);
            memberDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message, SUCCESS, VeinDeviceEventPersonTypeEnum.UPDATE_PERSON.getCode());
            // 失败
        } else {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("修改人员发生错误：" + message);
            memberDevice.setFailReason("修改人员发生错误：" + message);
            memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message, FAIL, VeinDeviceEventPersonTypeEnum.UPDATE_PERSON.getCode());
        }
    }

    private void addPerson(String message, VeinDeviceEvent event) {
        ResData resData = JSON.parseObject(message.substring(8), ResData.class);
        Long memberDeviceId = Long.parseLong(event.getBody());
        MemberDevice memberDevice = memberDeviceService.getById(memberDeviceId);
        if (memberDevice == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("人员设备关联关系已经不存在！ ");
            log.error("人员设备关联关系已经不存在！ 跳过执行 关联id【{}】", memberDeviceId);
            return;
        }
        MemberUser memberUser = memberUserService.getById(memberDevice.getMemberId());
        if (memberUser == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("人员已经不存在！");
            log.error("人员已经不存在！ 跳过执行 人员id【{}】", memberDevice.getMemberId());
            memberDevice.setFailReason("新增人员发生错误：人员已经不存在!" + message);
            memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog("", "", message, FAIL, VeinDeviceEventPersonTypeEnum.ADD_PERSON.getCode());
            return;
        }

        DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
        if (deviceBase == null) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("设备已经不存在！");
            log.error("设备已经不存在！ 跳过执行 设备id【{}】", memberDevice.getDeviceId());
            memberDevice.setFailReason("新增人员发生错误：设备已经不存在" + message);
            memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog(memberUser.getName(), "", message, FAIL, VeinDeviceEventPersonTypeEnum.ADD_PERSON.getCode());
            return;
        }
        // 成功
        if (ObjectUtil.equal(resData.getReturn_value(), 0)) {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_SUCCESS.getCode());
            memberDevice.setFailReason(null);
            memberDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
            memberDeviceService.updateById(memberDevice);
            List<MemberFeatures> features = featuresService.list(new LambdaQueryWrapper<MemberFeatures>()
                    .eq(MemberFeatures::getDwMemberUserId, memberUser.getId())
                    .eq(MemberFeatures::getPlatform, DeviceCategory.SUB_TYPE_PALM));
            replyService.createRelateInfo(memberUser, deviceBase, null, CollectionUtil.isNotEmpty(features));
            // 失败
        } else {
            event.setStatus(VeinDeviceEventStatusEnum.EXECUTE_ERROR.getCode());
            event.setFailReason("新增人员发生错误：" + message);
            memberDevice.setFailReason("新增人员发生错误：" + message);
            memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
            memberDeviceService.updateById(memberDevice);
//            deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message, FAIL, VeinDeviceEventPersonTypeEnum.ADD_PERSON.getCode());
        }

    }

}
